﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Logik_0._1._1
{
    public partial class MainWindow : Window
    {
        private int MaxColumn = 10;
        private int MaxLine = 20;

        private Logic logic = new Logic();

        public MainWindow()
        {
            InitializeComponent();

            logic.MaxColumn = MaxColumn;
            logic.MaxLine = MaxLine;

            StartGame();
        }

        public void StartGame()
        {
            DispatcherTimer timer = new DispatcherTimer();
            DispatcherTimer Ausgabe = new DispatcherTimer();

            int yStartPosition = 19;
            int xStartPosition = MaxColumn / 2;

            int xPosition = xStartPosition;
            int yPosition = yStartPosition;

            logic.Grid = new int[MaxColumn, MaxLine];

            timer.Interval = TimeSpan.FromMilliseconds(90);
            timer.Tick += (s, a) =>
            {
                int oldxPosition = xPosition;
                int oldyPosition = yPosition;

                #region Links/Rechts
                if (Keyboard.IsKeyDown(Key.D))
                {
                    if (xPosition < MaxColumn - 1)
                    {
                        xPosition++;
                    }
                    else
                    {
                        xPosition = 0;
                    }
                }

                if (Keyboard.IsKeyDown(Key.A))
                {
                    if (xPosition > 0)
                    {
                        xPosition--;
                    }
                    else
                    {
                        xPosition = MaxColumn - 1;
                    }
                }
                #endregion

                if (Keyboard.IsKeyDown(Key.S))
                {
                    if (yPosition < MaxLine - 1)
                    {
                        yPosition++;
                    }
                    else
                    {
                        oldxPosition = xStartPosition;
                        oldyPosition = yStartPosition;

                        xPosition = xStartPosition;
                        yPosition = yStartPosition;
                    }
                }

                #region Einsen Setzen
                if (logic.Grid[xPosition, yPosition] != 1 && yPosition < MaxLine)
                {
                    logic.Grid[oldxPosition, oldyPosition] = 0;
                    logic.Grid[xPosition, yPosition] = 1;
                }
                #endregion
            };
            timer.Start();

            #region Ausgabe
            Ausgabe.Interval = TimeSpan.FromMilliseconds(10);
            Ausgabe.Tick += (s, a) =>
            {
                Anzeige.Content = "\n";
                Console.Write(xPosition + "," + yPosition + "\n");

                for (int i = 0; i < logic.Grid.GetLength(1); i++)
                {
                    for (int j = 0; j < logic.Grid.GetLength(0); j++)
                    {
                        if (logic.Grid[j, i] == 1)
                        {
                            Anzeige.Content += "\u25A0";
                        }
                        else
                        {
                            Anzeige.Content += "\u25A1";
                        }
                    }
                    Anzeige.Content += ("\n");
                }
            };
            Ausgabe.Start();
            #endregion
        }
    }
}
